package study.springboot.mvc.controller._8_upload;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import study.eggs.result.Result;
import study.eggs.result.Results;
import study.springboot.mvc.service.upload.UploadService;

@Slf4j
@RestController
public class UploadController {

    /* M K Byte */
    private static final int FILE_MAX_SIZE = 1 * 1024 * 1024;

    @Autowired
    private UploadService uploadService;

    @PostMapping("/upload")
    @ResponseBody
    public Result upload(@RequestParam(value = "file", required = false) MultipartFile file) {
        //
        if (file == null || file.isEmpty()) {
           // throw new VException(ErrCode.PARAM_EMPTY, "file");
        }
        //
        String name = file.getName();
        String filename = file.getOriginalFilename();
        String contentType = file.getContentType();
        log.info("name= {}", name);
        log.info("filename= {}", filename);
        log.info("content_type= {}", contentType);
        //
        long size = file.getSize();
        log.info("file size (byte): {}", size);
        if (size > FILE_MAX_SIZE) {
           // throw new VException(ErrCode.BIZ_FILE_SIZE_OVER);
        }

        uploadService.upload(file);
        return Results.ok();
    }
}
